//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AmericanBasketPathPricer.h"
using namespace Cephei::QL::Pricingengines::Basket;
#include <gen/QL/Payoff.h>
#include <gen/QL/Methods/Montecarlo/MultiPath.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Methods::Montecarlo;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::CAmericanBasketPathPricer (UInt64 assetNumber, Cephei::QL::IPayoff^ payoff, Microsoft::FSharp::Core::FSharpOption<UInt64>^ polynomOrder, Microsoft::FSharp::Core::FSharpOption<QL::Methods::Montecarlo::LsmBasisSystem::PolynomTypeEnum>^ polynomType) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CPayoff^ _Cpayoff;
    try
    {
#ifdef HANDLE
        _phAmericanBasketPathPricer = NULL;
#endif
        QuantLib::Size _assetNumber = (QuantLib::Size)ValueHelper::Convert (assetNumber); //d
        _Cpayoff = safe_cast<CPayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::Payoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::Payoff>&> (_Cpayoff->GetShared ()); 
        QuantLib::Size _polynomOrder = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (polynomOrder) ? (QuantLib::Size)ValueHelper::Convert (polynomOrder->Value) : 2); //4
        QuantLib::LsmBasisSystem::PolynomType _polynomType = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Methods::Montecarlo::LsmBasisSystem::PolynomTypeEnum>::IsSome::get (polynomType) ? (QuantLib::LsmBasisSystem::PolynomType)polynomType->Value : QuantLib::LsmBasisSystem::PolynomType::Monomial); //10
        _ppAmericanBasketPathPricer = new boost::shared_ptr<QuantLib::AmericanBasketPathPricer> (new QuantLib::AmericanBasketPathPricer ( _assetNumber,  _payoff,  _polynomOrder,  _polynomType ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
    }
}
Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::CAmericanBasketPathPricer (boost::shared_ptr<QuantLib::AmericanBasketPathPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanBasketPathPricer = NULL;
#endif
	_ppAmericanBasketPathPricer = &childNative;
    
}
Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::CAmericanBasketPathPricer (QuantLib::AmericanBasketPathPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAmericanBasketPathPricer = NULL;
#endif
	_ppAmericanBasketPathPricer = new boost::shared_ptr<QuantLib::AmericanBasketPathPricer> (&childNative);
    
    _AmericanBasketPathPricerOwner = owner;
    
}

Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::CAmericanBasketPathPricer (CAmericanBasketPathPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanBasketPathPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAmericanBasketPathPricer = new boost::shared_ptr<QuantLib::AmericanBasketPathPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::CAmericanBasketPathPricer (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanBasketPathPricer = NULL;
#endif
	if (!t->IsSubclassOf(CAmericanBasketPathPricer::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::CAmericanBasketPathPricer (QuantLib::Handle<QuantLib::AmericanBasketPathPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanBasketPathPricer = &childNative;
	_ppAmericanBasketPathPricer = &static_cast<boost::shared_ptr<QuantLib::AmericanBasketPathPricer>>(childNative.currentLink());
    
    _AmericanBasketPathPricerOwner = owner;
}
Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::CAmericanBasketPathPricer (QuantLib::Handle<QuantLib::AmericanBasketPathPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAmericanBasketPathPricer = &childNative;
	_ppAmericanBasketPathPricer = &static_cast<boost::shared_ptr<QuantLib::AmericanBasketPathPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::CAmericanBasketPathPricer (QuantLib::AmericanBasketPathPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAmericanBasketPathPricer = NULL;
#endif
	_ppAmericanBasketPathPricer = new boost::shared_ptr<QuantLib::AmericanBasketPathPricer> (new QuantLib::AmericanBasketPathPricer (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::~CAmericanBasketPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAmericanBasketPathPricer != NULL)
    {
	    delete _ppAmericanBasketPathPricer;
        _ppAmericanBasketPathPricer = NULL;
    }
}
Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::!CAmericanBasketPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAmericanBasketPathPricer != NULL)
    {
	    delete _ppAmericanBasketPathPricer;
    }
}
QuantLib::AmericanBasketPathPricer& Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::GetReference ()
{
    if (_ppAmericanBasketPathPricer == NULL) throw REFNEW NativeNullException ();
	return **_ppAmericanBasketPathPricer;
}
boost::shared_ptr<QuantLib::AmericanBasketPathPricer>& Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::GetShared ()
{
    if (_ppAmericanBasketPathPricer == NULL) throw REFNEW NativeNullException ();
	return *_ppAmericanBasketPathPricer;
}
QuantLib::AmericanBasketPathPricer* Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::GetPointer ()
{
    if (_ppAmericanBasketPathPricer == NULL) throw REFNEW NativeNullException ();
	return &**_ppAmericanBasketPathPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AmericanBasketPathPricer>& Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::GetHandle ()
{
	if (_phAmericanBasketPathPricer == NULL)
	{
		_phAmericanBasketPathPricer = new Handle<QuantLib::AmericanBasketPathPricer> (*_ppAmericanBasketPathPricer);
	}
	return *_phAmericanBasketPathPricer;
}
#endif
bool Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::HasNative () 
{
	return (_ppAmericanBasketPathPricer != NULL);
}

Cephei::QL::Math::IArray^ Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer::State (Cephei::QL::Methods::Montecarlo::IMultiPath^ path, UInt64 t)
{
    CMultiPath^ _Cpath;
    try
    {
        _Cpath = safe_cast<CMultiPath^> (path);
        _Cpath->Lock();
        QuantLib::MultiPath& _path = static_cast<QuantLib::MultiPath&> (_Cpath->GetReference ()); 
        QuantLib::Size _t = (QuantLib::Size)ValueHelper::Convert (t); //a
    	QuantLib::Array _rv = (QuantLib::Array)(*_ppAmericanBasketPathPricer)->state ( _path,  _t );   
        Cephei::QL::Math::CArray^ _nrv = REFNEW Cephei::QL::Math::CArray (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpath != nullptr) _Cpath->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Basket::IAmericanBasketPathPricer^ Cephei::QL::Pricingengines::Basket::CAmericanBasketPathPricer_Factory::Create (UInt64 assetNumber, Cephei::QL::IPayoff^ payoff, Microsoft::FSharp::Core::FSharpOption<UInt64>^ polynomOrder, Microsoft::FSharp::Core::FSharpOption<QL::Methods::Montecarlo::LsmBasisSystem::PolynomTypeEnum>^ polynomType)
{
    return REFNEW CAmericanBasketPathPricer ( assetNumber,  payoff,  polynomOrder,  polynomType);
}
